# -*- coding: utf-8 -*-

import os
import pymongo as pm;
import json
from items import *
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

# CREATE TABLE `kejian` (
# `id` int(11) NOT NULL auto_increment comment ‘课件id’,
# `subject` varchar(50) default NULL comment ‘科目’,
# `edtion` varchar(100) default NULL comment ‘教材版本’,
# `grade` varchar(50) default NULL comment ‘年级’,
# `term` varchar(50) default NULL comment ‘上下学期’,
# `chater` varchar(100) default NULL comment ‘章名称’,
# `unit` varchar(100) default NULL comment ‘单元名称’,
# `section` varchar(100) default NULL comment ‘节名称’,
# `sources` varchar(5) default NULL comment ‘课件来源’,
# `title` varchar(200) default NULL comment ‘课件标题’,
# `rootdir` varchar(255) default NULL comment ‘课件物理存储路劲’,
# `uri` varchar(255) default NULL comment ‘下载地址uri’,
# PRIMARY KEY (`id`),
# UNIQUE KEY `index_sources_uri` USING BTREE (`sources`,`uri`)
# ) ENGINE=InnoDB AUTO_INCREMENT=692740 DEFAULT CHARSET=utf8;




class XuekubaoPipeline(object):

    def __init__(self):
        self.path="";
        #self.client = pm.MongoClient(host='127.0.0.1', port=27017)
       # self.client=pm.MongoClient('mongodb://tiku:tiku123@127.0.0.1:27017/tiku');
       # self.db = self.client['tiku'];
        #self.paper = open("work/serv/scrapy" + '/' + "paper", 'wb');
        #self.shiti=open("work/serv/scrapy"+'/'+'shiti','wb')
        self.xuekepaper=open('/serv/scrapy'+'/'+'xuekepaper','wb');

        return;

    def process_item(self,item,spider):
        #line=json.dumps(dict(item));
        if(isinstance(item,XuekubaoItem)):
           self.process_xuekebao_item(item);
        elif (isinstance(item, Shiji21Item)):
            self.process_shiji_item(item);
        elif (isinstance(item, Shiji21PaperItem)):
            self.process_paper_item(item);
        elif (isinstance(item, HdItem)):
            self.process_hd_item(item);
        elif (isinstance(item ,XueKeWangItem)):
            self.process_xueke_item(item)
        return;

    def process_xuekebao_item(self,item):
        col = self.db['xuekubao'];
        col.insert(item);

    def process_hd_item(self, item):
        col = self.db['hd'];
        col.insert(item);

    def process_xueke_item(self,item):
        jsontext=json.dumps(dict(item), ensure_ascii=False) + ",\n";
        self.xuekepaper.write(jsontext)
    def process_shiji_item(self, item):
        #col = self.db['shiji211'];
        #col.insert(item);
        jsontext = json.dumps(dict(item), ensure_ascii=False) + ",\n"
        self.shiti.write(jsontext.encode("utf-8"))

    def process_paper_item(self,item):
        #col=self.db['shiji_paper']
        #col.insert(item)
        jsontext = json.dumps(dict(item), ensure_ascii=False) + ",\n"
        self.paper.write(jsontext.encode("utf-8"))

    def close_spider(self, spider):
        self.paper.close()
        self.shiti.close()



